<?php

include 'utilities.php';

printxmlRequest('search');
parseXMLStr();
printxmlResponse('search');

function parseXMLStr()
{

	global $HTTP_RAW_POST_DATA, $xmlDoc, $xmlResponse, $xmlMessage, $result, $debug;

	$dirList = $xmlDoc->getElementsByTagName("dir");

	foreach ($dirList as $dir) {
		$tagname = htmlspecialchars($dir->nodeValue);

		if($tagname == "/")
		{
			$xmlResponse .= "<dirtree>";
			$xmlResponse .= exec("tag -xml getwholelist");
			$xmlResponse .= "</dirtree>";
		}
		else if($tagname !="")
		{
			$xmlResponse .= "<files>";
			$xmlResponse .= exec("tag -xml hierarchical \"$tagname\"");
			$xmlResponse .= "</files>";
		}
	}

	$fileList = $xmlDoc->getElementsByTagName("filename");
	foreach ($fileList as $file) {
		$filename = htmlspecialchars($file->nodeValue);

		$patterns = array ('/~/', '/ /');
		$replace = array ('%7E', '%20');
		ksort($patterns); ksort($replace);
		$filename =  preg_replace($patterns, $replace, $filename);
		
		//$xmlResponse .= "<file>$filename</file>";
		$xmlResponse .= "<tagslist>";
		$xmlResponse .= exec("tag -xml listtags \"$filename\"");
		$xmlResponse .= "</tagslist>";
	}


	$cmdLst = $xmlDoc->getElementsByTagName("command");
	$tagLst = $xmlDoc->getElementsByTagName("tag");
	$fileLst = $xmlDoc->getElementsByTagName("file");
	foreach ($cmdLst as $cmd) {
	foreach ($tagLst as $tag) {
	foreach ($fileLst as $file) {
		$cmdname = $cmd->nodeValue;
		$filename = myURLencode($file->nodeValue);
		$filename = rtrim($filename, '/');
		$tagname = $tag->nodeValue;

	
		$command = "tag \"$cmdname\" \"$filename\" \"$tagname\"";
		$xmlResponse .= "<command>".$command."</command>";
		$xmlResponse .= "<file>".$filename."</file>";
		$xmlResponse .= "<tag>".$tagname."</tag>";
		printxmlResponse("command");

		$resp = exec($command);
	}
	}
	}
}

function myURLencode($filename)
{
	$filename = htmlspecialchars($filename);
	$patterns = array ('/~/', '/ /');
	$replace = array ('%7E', '%20');
	ksort($patterns); ksort($replace);
	return preg_replace($patterns, $replace, $filename);
}

?>
